<?php
// open_conn();
if ($a == 'save') {
    $username = I('username');
    $trunk = I('trunk');
    $agent = I('agent');
    $ip=I('ip');
    $expires_time=I('expires_time');
    if (!$username || !$trunk || !$agent || !$ip) {
        result(false, '表单数据不完整!');
    }
    if(!is_numeric($trunk) || !is_numeric($agent)){
        result(false, '表单数据有误!');
    }
    $filename = $username . '.txt';
    $content=encrypt_rnd("username=$username,trunk=$trunk,agent=$agent,expires_time=$expires_time,ip=$ip");
    $content='??'.str_replace('==','??',$content);
    // $path = './Licence/' . $filename;
    // file_put_contents($path, $content);
    // system("/root/test 1 $filename");
    // show_save_file_dialog("$username.txt", file_get_contents($path));
    sofunc_save_file_dialog($filename, $content);
    result('生成成功!', '?success');
}
// 2014-10-15 by sutron
function update_cet_record($username,$ip,$trunk,$agent,$expires_time){
    $result=mysql_query("select id from cet where ip_address='$ip'");
    $row=mysql_fetch_array($result,MYSQL_ASSOC);
    if(!$row){
        $result=mysql_query("insert into cet (username, frist_runtime, ip_address, max_trunk, max_agent, status, expires_time) values ('$username', now(), '$ip', $trunk, $agent, 1, '$expires_time');");
    }else{
        $result=mysql_query("update cet set username='$username', frist_runtime=now(), max_trunk=$trunk, max_agent=$agent, status=1, expires_time='$expires_time' where ip_address='$ip'");
    }
    if($result===false){
        result(false,mysql_error());
    }
}
?>
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
    <title>授权文件生成器</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style type="text/css">
        html { font-size: 12px; }
        form, li, ul { margin: 0px; }
        li { list-style: none; }
        fieldset { margin-bottom: 10px;}
        fieldset.nobor { border:none;}
        .table-list { border:none; border-right: solid 1px #CCC; border-bottom:solid 1px #CCC;}
        .table-list th, .table-list td { border:none; border-left: solid 1px #CCC; border-top:solid 1px #CCC; font-weight: normal; text-align: left; padding: 5px;}
        .copyright { margin-top: 10px; color: #666; }
    </style>
</head>
<body>
<a href="?m=login&a=login_out">[注销]</a>
<fieldset>
    <legend>授权文件生成器</legend>
    <form action="#" method="post">
        <ul>
            <li>
                <label>用户名：</label>
                <input type="text" name="username" placeholder="请输入用户名" title="请填写用户名" required/>
            </li>
            <li>
                <label>IP地址：</label>
                <input type="text" name="ip" placeholder="请输入IP地址" title="请填写IP地址" required/> <?php echo '本机IP:'.$_SERVER['SERVER_NAME']; ?>
            </li>
            <li>
                <label>最大用户数：</label>
                <input type="text" name="agent" placeholder="请输入最大用户数" title="请填写最大用户数" value="300" required/>
            </li>
            <li>
                <label>最大外呼数：</label>
                <input type="text" name="trunk" placeholder="请输入最大外呼数" title="请填写最大外呼数" value="1000" required/>
            </li>
            <li>
                <label>过期时间：</label>
                <input type="datetime" name="expires_time" placeholder="请输入过期时间" title="请填写过期时间" value="3000-1-1 0:0:0" required/>
                <span>格式：2014-10-13 18:15:30</span>
            </li>
            <li>
                <label>&nbsp;</label>
                <input type="submit" value="保存"/><input type="reset" value="重置"/>
            </li>
        </ul>
        <input type='hidden' name='a' value='save'/>
    </form>
</fieldset>
<fieldset class="nobor">
    <legend>客户机列表</legend>
    <div style="max-height: 300px; overflow-y: scroll;">
        <?php 
        // echo show_table_text('select * from cet'); 
        ?>
    </div>
</fieldset>
<div class="copyright">(c) 2014 XCall</div>
</body>
</html>